﻿using System.Drawing;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The magnifier zoom, first clic zoom in, second clic back to previous view
    /// </summary>
    public class TMagnifier : BaseTool
    {
        #region Fields
        bool _MouseDown;
        Point _InitialMouseLocation;
        RectangleD _InitialObjectView;
        double Zoomfactor;
        Point2D64D _InitialMouseObjectCoordinates;//=inversetransform(_InitialMouseLocation)
        RectangleD _TempObjectView;
        Point2D64D _TranslationVector;//The translation vector so the point clic is the center of the zoom

        #endregion

        #region Constructor
        /// <summary>
        /// The zoom tool, clic and drag towards the top of the screen will zoom in,
        /// towards the bottom will zoom out
        /// </summary>
        /// <param name="Viewport">The viewport the tool with act on</param>
        public TMagnifier(BaseViewport Viewport)
            : base(Viewport,"Magnifier")
        {
            _MouseDown = false;
        }
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            _MouseDown = !_MouseDown;

            _InitialMouseLocation = MouseArgs.Location;
            _InitialObjectView = Viewport.CurrentObjectView;
            _InitialMouseObjectCoordinates = Viewport.Object2ImageTransformation.InverseTransform(_InitialMouseLocation);

            Zoomfactor = Vars.Setup.Tools.TMagnifier.ZoomFactor;
            SendMessage("Click to zoom in");
            if (_MouseDown)
            {
                Zoomfactor = 1 / Zoomfactor;
                SendMessage("Click to zoom out");
            }

            Viewport.CurrentObjectView = RectangleD.FromCenter(
                _InitialObjectView.Center,
                _InitialObjectView.Width * Zoomfactor,
                _InitialObjectView.Height * Zoomfactor,
                YAxisDirection.UpSide);

            _TranslationVector = _InitialMouseObjectCoordinates - Viewport.Object2ImageTransformation.InverseTransform(_InitialMouseLocation);

            _TempObjectView = Viewport.CurrentObjectView;
            _TempObjectView.X += _TranslationVector.X;
            _TempObjectView.Y += _TranslationVector.Y;
            Viewport.CurrentObjectView = _TempObjectView;

            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click to zoom in");
        } 
        #endregion
    }
}
